package com.zrz.controller;

import com.zrz.model.auto.Record;
import com.zrz.model.auto.User;
import com.zrz.param.UserParam;
import com.zrz.service.MineService;
import com.zrz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mine")
public class MineController {
    @Autowired
    private UserService userService;
    @Autowired
    private MineService mineService;
    @RequestMapping("/account")
    public String accountINfo(HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
//        Integer userId = 197;
        UserParam userParam=new UserParam();
        User user=userService.getUserById(userId);
        model.addAttribute("user",user);
        model.addAttribute("userParam",user);
        return "mine/accountInfo";
    }

    @RequestMapping("/contact")
    public String contact(HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        List<User> userList=userService.refferUser(user.getId());
        model.addAttribute("userList",userList);
        return "mine/contact";
    }

    /**
     * 领酒记录
     * @param
     * @return
     */
    @RequestMapping("/drawWine")
    public String drawWine(HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        List<Record> recordList=userService.getRecord(user.getId());
        model.addAttribute("recordList",recordList);
        return "mine/drawWine";
    }

    @RequestMapping("/editAccountInfo")
    public String editAccountInfo(HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
//        Integer userId = 197;
        User user=userService.getUserById(userId);
        model.addAttribute("userParam",user);
        return "mine/editAccountInfo";
    }

    @RequestMapping("/myStore")
    public String myStore( HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        user=userService.getUserInfo(user.getId());
        model.addAttribute("user",user);
        return "mine/myStore";
    }

    @RequestMapping("/myWallet")
    public String myWallet(Integer id) {
        return "mine/myWallet";
    }

    @RequestMapping("/walletDetail")
    public String walletDetail(Integer id) {
        return "mine/walletDetail";
    }
    @PostMapping("/update")
    @ResponseBody
    public Map updateUser(@ModelAttribute(value = "userParam") UserParam userParam, HttpServletRequest request, Model model) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
//        Integer userId = 197;
        User user=userService.getUserById(userId);
        user=userService.updateUser(userParam,user);
        model.addAttribute("user",user);
        model.addAttribute("userParam",user);
        request.getSession().setAttribute("user",user);
        Map map=new HashMap();
        map.put("code","1");
        return map;
    }

    @ResponseBody
    @RequestMapping("/card")
    public Map vipByCardNum(HttpServletRequest request,String code){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        Map map=mineService.vipByCardNum(user,code);
        return map;
    }


}
